package com.TaskOrganizer.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.TaskOrganizer.core.AppCore;
import com.TaskOrganizer.managers.TaskPriority;
import com.TaskOrganizer.task.Task;

public class AddTaskDialog extends JFrame {

	private static final long serialVersionUID = 3075745342221399918L;
	private JPanel priorityPanel;
	private JLabel descriptionLabel, nameLabel, priorityLabel;
	private JTextArea descriptionTextArea, nameTextArea;
	private JRadioButton highPriority, normalPriority;
	private JButton confirm, cancel;
	private JScrollPane descriptionScrollPane, nameScrollPane;
	private boolean update;
	private Task task = null;

	public AddTaskDialog() {
		super("Add task");
		update = false;
		setup();
	}

	public AddTaskDialog(Task task) {
		super("Update task");
		update = true;
		this.task = task;
		setup();
		nameTextArea.setText(task.getName());
		descriptionTextArea.setText(task.getDescription());
		if (task.getPriority() == TaskPriority.HIGH) {
			highPriority.setSelected(true);
			normalPriority.setSelected(false);
		}
	}

	public void setup() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
		Toolkit kit = Toolkit.getDefaultToolkit();
		URL url = getClass().getResource("/com/TaskOrganizer/icons/icon.png");
		Image img = kit.createImage(url);
		setIconImage(img);
		setLayout(new GridLayout(4, 2));

		getNamePanel();
		getDescriptionPanel();
		getPriorityPanel();
		getBottomPanel();

		pack();
		Dimension dim = kit.getScreenSize();
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);
		setResizable(false);
	}

	private void getBottomPanel() {
		if (confirm == null)
			confirm = new JButton("Confirm");
		if (cancel == null)
			cancel = new JButton("Cancel");
		confirm.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (update) {
					getTask().setName(nameTextArea.getText().trim());
					getTask().setDescription(
							descriptionTextArea.getText().trim());
					getTask().setPriority(getSelectedPriority());
					AppCore.getInstance().saveTaskList();
					new TaskDialog(getTask());
				} else {
					AppCore.getInstance().addTask(
							new Task(nameTextArea.getText().trim(),
									descriptionTextArea.getText().trim(),
									getSelectedPriority()));
				}
				dispose();
			}

		});
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (task != null)
					new TaskDialog(task);
				dispose();
			}
		});

		add(confirm);
		add(cancel);
	}

	private Task getTask() {
		return task;
	}

	private TaskPriority getSelectedPriority() {
		if (highPriority.isSelected())
			return TaskPriority.HIGH;
		else
			return TaskPriority.NORMAL;
	}

	private void getPriorityPanel() {
		if (priorityLabel == null)
			priorityLabel = new JLabel("Priority: ");

		if (highPriority == null)
			highPriority = new JRadioButton("High");
		if (normalPriority == null)
			normalPriority = new JRadioButton("Normal");

		highPriority.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				normalPriority.setSelected(false);
			}
		});
		normalPriority.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				highPriority.setSelected(false);
			}
		});
		if (priorityPanel == null)
			priorityPanel = new JPanel();
		normalPriority.setSelected(true);
		priorityPanel.add(highPriority);
		priorityPanel.add(normalPriority);

		add(priorityLabel);
		add(priorityPanel);
	}

	private void getDescriptionPanel() {
		if (descriptionLabel == null)
			descriptionLabel = new JLabel("Description: ");
		if (descriptionTextArea == null) {
			descriptionTextArea = new JTextArea(5, 10);
			descriptionTextArea.setLineWrap(true);
		}
		if (descriptionScrollPane == null) {
			descriptionScrollPane = new JScrollPane(descriptionTextArea);
			descriptionScrollPane
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		}
		add(descriptionLabel);
		add(descriptionScrollPane);
	}

	private void getNamePanel() {
		if (nameLabel == null)
			nameLabel = new JLabel("Task name: ");
		if (nameTextArea == null) {
			nameTextArea = new JTextArea(5, 10);
			nameTextArea.setLineWrap(true);
		}
		if (nameScrollPane == null) {
			nameScrollPane = new JScrollPane(nameTextArea);
			nameScrollPane
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		}
		add(nameLabel);
		add(nameScrollPane);
	}
}
